package com.example.covid19.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.covid19.common.Dto.goodsDto;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.Goods;
import com.example.covid19.entity.Gstock;
import com.example.covid19.entity.Picp;
import com.example.covid19.mapper.GoodsMapper;
import com.example.covid19.mapper.GstockMapper;
import com.example.covid19.mapper.PicpMapper;
import com.example.covid19.service.GoodsService;
import com.example.covid19.service.GstockService;
import com.example.covid19.service.ManufacturerService;
import com.example.covid19.service.PicpService;
import com.example.covid19.util.SecretUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author BaiZhengChun
 * @since 2022-09-12
 */
@Slf4j
@RestController
@RequestMapping("/goods")
public class GoodsController {
    private final GoodsService goodsService;
    private final GstockService gstockService;

    private final GstockMapper gstockMapper;
    private final PicpService picpService;
    private final GoodsMapper goodsMapper;
    private final PicpMapper picpMapper;
    private final ManufacturerService manufacturerService;
    @Autowired
    SecretUtil secretUtil;

    public GoodsController(GoodsService goodsService, GstockService gstockService, GstockMapper gstockMapper, PicpService picpService, GoodsMapper goodsMapper, PicpMapper picpMapper, ManufacturerService manufacturerService) {
        this.goodsService = goodsService;
        this.gstockService = gstockService;
        this.gstockMapper = gstockMapper;
        this.picpService = picpService;
        this.goodsMapper = goodsMapper;
        this.picpMapper = picpMapper;
        this.manufacturerService = manufacturerService;
    }

    public String getbase(String picid) throws FileNotFoundException {
        return goodsService.getbase(picid);
    }

    @RequestMapping("/goodsinfo")
    public Result goodsinfo(@RequestParam("pageNo") Integer pageNo,@RequestParam("pagesize")Integer pagesize) throws FileNotFoundException {
        int a = (pageNo-1)*pagesize;
        return goodsService.goodsinfo(a,pagesize);
    }

    @RequestMapping("/editgoods")
    public Result editgoods(@RequestBody goodsDto goodsDto) {
        return goodsService.editgoods(goodsDto);
    }

    @RequestMapping("/edpic")
    public Result edpic(@RequestParam("pic") MultipartFile multipartFile, @RequestParam("sid") String str2) {
        return goodsService.edpic(multipartFile, str2);

    }
    @RequestMapping("/getuu")
    public Result getuu() {
        String uuid = UUID.randomUUID().toString();
        return Result.success(200,"成功",uuid);

    }
    @RequestMapping("/upgoo")
    public Result upgoo(@RequestBody goodsDto goodsDto) {
        System.out.println(goodsDto);
            Goods goods = new Goods();
            goods.setGoodsname(goodsDto.getGoodsname());
            goods.setType(goodsDto.getType());
            goods.setQuality(goodsDto.getQuality());
            goods.setBrand(goodsDto.getBrand());
            goods.setPrice(goodsDto.getPrice());
            goods.setPrice(goods.getPrice());
            goods.setProducingArea(goodsDto.getProducingArea());
            goods.setPicid(goodsDto.getPicid());

            boolean flag = goodsService.save(goods);
                if (flag){
                    boolean flag2;

                    return Result.success(200,"提交成功",null);
                }
                else {
                    return Result.fail(400,"失败",null);
                }
    }
    @RequestMapping("/uppicid")
    public Result uppicid(@RequestParam("pic") MultipartFile multipartFile, @RequestParam("sid") String str2) {
        String str = upLoad(multipartFile);
        Picp picp = new Picp();
        picp.setPicpath(str);
        picp.setPicid(str2);
        System.out.println(picp);
        boolean flag = picpService.save(picp);
        if (flag){
            return Result.success(200,"成功",null);
        } else {
            System.out.println("fewsfwe");
            return Result.success(400,"faf",null);
        }

    }
    @RequestMapping("/loading")
    public Result loading(@RequestParam("count") Integer count,@RequestParam("pic") String pic) {
       Goods goods = goodsService.getOne(new QueryWrapper<Goods>().eq(
               "picid",pic
       ));
       Gstock gstock = gstockService.getOne(new QueryWrapper<Gstock>().eq(
               "picid",pic
       ));
       int i = gstock.getStock();
       int j = goods.getQuantity();
       if (i<count){
           return Result.fail(400,"库存不够，需要采购",null);
       }
       gstock.setStock(i-count);
       goods.setQuantity(j+count);
       gstockService.updateById(gstock);
       goodsService.updateById(
               goods);
       return Result.success(200,"成功",null);

    }
    @RequestMapping("/delgo")
    public Result delgo(@RequestParam("sid") Integer sid,@RequestParam("pic") String pic,@RequestParam("quantity") Integer quantity) {
        Goods goods = goodsService.getOne(new QueryWrapper<Goods>().eq(
                "sid", sid
        ));
        Gstock gstock = gstockService.getOne(new QueryWrapper<Gstock>().eq(
                "picid",pic
        ));
        if (quantity>0){
            int i = gstock.getStock();
            gstock.setStock(quantity+i);
        }
        goodsMapper.deleteById(goods);
        gstockService.updateById(gstock);
        return Result.success(200,"成功",null);

    }

    public String upLoad(MultipartFile multipartFile) {
        return goodsService.upLoad(multipartFile);

    }
}
